// pages/mine/mine.js
var CryptoJS = require('crypto-js');
function decryptData(encryptedData, sessionKey, iv) {
  const sessionKeyWordArray = CryptoJS.enc.Base64.parse(sessionKey);
  const ivWordArray = CryptoJS.enc.Base64.parse(iv);
  const encryptedDataWordArray = CryptoJS.enc.Base64.parse(encryptedData);
  const decrypted = CryptoJS.AES.decrypt(
      {
          ciphertext: encryptedDataWordArray
      },
      sessionKeyWordArray,
      {
          iv: ivWordArray,
          mode: CryptoJS.mode.CBC,
          padding: CryptoJS.pad.Pkcs7
      }
  );
  const decryptedData = decrypted.toString(CryptoJS.enc.Utf8);
  return decryptedData;
}

Component({
  data: {
    userName:"王晓摆",
    companyName:"北京大学第三医院",
  },
  methods: {
    toWebsite(e){
      wx.navigateTo({
        url: '/pages/mine/mine',
      })
    }
  },
  pageLifetimes: {
    show() {
      if (typeof this.getTabBar === 'function') {
        this.getTabBar((tabBar) => {
          tabBar.setData({
            selected: 3
          })
        })
      }
      wx.login({
        success: function(res) {
          var appid = 'wxce7ffee86fc49962';
          var secret = '172c5e8a0d190b36a1d8d642f07ed444';
          if (res.code) {
            wx.request({
              url: 'https://39mx35735tt8.vicp.fun/sns/jscode2session?appid=' +
                appid +
                '&secret=' + secret + '&js_code=' + res.code +
                '&grant_type=authorization_code',
              header: {
                'content-type': 'json'
              },
              success: function(res) {
                var session_key = res.data.session_key;
                wx.getSetting({
                  success: function(res) {
                    wx.getUserInfo({
                      withCredentials: true,
                      success: function (userInfoRes) {
                        const decryptedData = decryptData(userInfoRes.encryptedData, session_key, userInfoRes.iv);
                        console.log(decryptedData)
                      }
                    })
                  }
                })
              }
            })
          }
        }
      })
    }
  }
})